<?php

namespace App\Jobs;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\GpsadvUsers;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Jobs\ProcessSendNotify;
use Illuminate\Support\Facades\Response;

class ProcessDealLocation implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {

        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->batch()->cancelled()) {
            // 确定批次是否已取消..
            return;
        }

        //检测该用户是否有未过期的广告
//        $advers_condition = [["expire_end_time","<",time()],["deleted_at",NUll]];

        $advers = GpsadvUsers::where("android_id",$this->data['android_id'])
            ->first()->advers
            ->where("expire_end_time",">=",date("Y-m-d H:i:s",time()))
            ->where("deleted_at","=",null)
            ->toArray();
        Log::info('ProcessDealLocation: {advers}', ['advers'=>$advers]);
        if(count($advers) > 0){
//            dump('fdsfds1111');
//            $radiusmember = Redis::georadiusbymember("position_list",$this->data['mobile_phone'],"140","km");
            $radiusmember = Redis::georadius("position_list",$this->data['lotitude'],$this->data['latitude'],"140","km",["withdist"=>"10 asc"]);
            Log::info('ProcessDealLocation: {radiusmember}', ['radiusmember'=>$radiusmember]);
            ProcessSendNotify::dispatch($radiusmember,$advers);
        }else{
//            dump('fdsfds222');
            return;
        }
    }
}
